/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.model.elem;

import chunmap.model.coord.Position;


/**
 * Envelope的建造者，用于优化Envelope的操作，类似于String与StringBuilder的关系。
 * 
 * @author chunquedong
 * 
 */
public class EnvelopeBuf extends Envelope{
	public EnvelopeBuf() { super(); }
    public EnvelopeBuf(double x1, double y1, double x2, double y2) {
    	super(x1, y1, x2, y2); 
    }
    public EnvelopeBuf(Envelope envelop){
    	this(envelop.minX, envelop.minY, envelop.maxX, envelop.maxY);
    }

    // -----------------------------------------------------------------------

    public void extendEnvelop(Position point)
    {
        double x = point.getX();
        double y = point.getY();

        if (isNone())
        {
            minX = x;
            maxX = x;
            minY = y;
            maxY = y;

            return;
        }

        if (x < minX)
            minX = x;
        else if (x > maxX)
            maxX = x;

        if (y < minY)
            minY = y;
        else if (y > maxY)
            maxY = y;

    }
    public void mergeEnvelop(Envelope envelop)
    {

        if (envelop.minX < minX)
            minX = envelop.minX;
        if (envelop.minY < minY)
            minY = envelop.minY;

        if (envelop.maxX > maxX)
            maxX = envelop.maxX;
        if (envelop.maxY > maxY)
            maxY = envelop.maxY;
    }
    public void mergeEnvelop(EnvelopeBuf envelop)
    {

        if (envelop.minX < minX)
            minX = envelop.minX;
        if (envelop.minY < minY)
            minY = envelop.minY;

        if (envelop.maxX > maxX)
            maxX = envelop.maxX;
        if (envelop.maxY > maxY)
            maxY = envelop.maxY;
    }

    public void bufferEnvelop(double d)
    {
        minX -= d;
        minY -= d;
        maxX += d;
        maxY += d;
    }
    public void moveEnvelop(double x, double y)
    {
        minX += x;
        minY += y;
        maxX += x;
        maxY += y;
    }

    public Envelope toEnvelop()
    {
        return new Envelope(minX, minY, maxX, maxY);
    }

    public EnvelopeBuf clone()
    {
        return new EnvelopeBuf(minX, minY, maxX, maxY);
    }

}